package com.wen.center.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Created by LAIO on 2018/5/27.
 */
@PropertySources({
        @PropertySource("classpath:mysql.properties")
})
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackageClasses = MysqlParams.class)
public class MySqlConfig {

//    @Bean
//    public DataSource dataSource(MysqlParams mysqlParams) throws PropertyVetoException {
//        ComboPooledDataSource datasource = new ComboPooledDataSource();
//        datasource.setDriverClass(mysqlParams.getDriverclass());
//        datasource.setJdbcUrl(mysqlParams.getUrl());
//        datasource.setUser(mysqlParams.getUsername());
//        datasource.setPassword(mysqlParams.getPassword());
//        return datasource;
//    }

    @Bean
    public DataSource dataSource(MysqlParams mysqlParams) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(mysqlParams.getUrl());
        datasource.setDriverClassName(mysqlParams.getDriverclass());
        datasource.setUsername(mysqlParams.getUsername());
        datasource.setPassword(mysqlParams.getPassword());
        return datasource;
    }

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setTypeAliasesPackage("com.wen.center");
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml"));
        return sqlSessionFactory;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.wen.center");

        return mapperScannerConfigurer;
    }

    @Bean
    @javax.annotation.Resource(name = "sqlSessionFactory")
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactoryBean sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory.getObject());
    }
}
